<template>
  <view class="comBox">
    <sickTitleCom></sickTitleCom>
    <view v-for="(que, index) in quesList" :key="index">
      <radioComNoNextBtn
        v-if="
          que.pageNum === 8 ||
          que.pageNum === 10 ||
          (que.pageNum >= 12 && que.pageNum <= 16) ||
          que.pageNum === 18 ||
          que.pageNum === 19
        "
        :info="que"
        @choseList="choseList"
      ></radioComNoNextBtn>
      <radioComInpSmokeNoNextBtn
        v-if="que.pageNum === 9"
        :info="que"
        @choseList="choseList"
      ></radioComInpSmokeNoNextBtn>
      <checkComNoNextBtn v-if="que.pageNum === 11" :info="que" @choseList="choseList"></checkComNoNextBtn>
      <radioComInpPlayNoNextBtn v-if="que.pageNum === 17" :info="que" @choseList="choseList"></radioComInpPlayNoNextBtn>
    </view>

    <!-- 下一题上一题按钮 -->
    <nextProBtnCom @nextBtn="nextBtn" @proBtn="proBtn"></nextProBtnCom>
  </view>
</template>

<script>
import nextProBtnCom from '@/components/nextProButton';
import radioComNoNextBtn from './radioComNoNextBtn';
import checkComNoNextBtn from './checkComNoNextBtn';
import radioComInpSmokeNoNextBtn from './radioComInpSmokeNoNextBtn';
import radioComInpPlayNoNextBtn from './radioComInpPlayNoNextBtn';
import sickTitleCom from './sickTitleCom';
import { ajaxSend } from './ajaxHealth.js';
import curIcon from '@/static/icon/cur.png';
export default {
  components: {
    // 上一题下一题组件
    nextProBtnCom,
    // 疾病大类title
    sickTitleCom,
    radioComNoNextBtn,
    radioComInpSmokeNoNextBtn,
    checkComNoNextBtn,
    radioComInpPlayNoNextBtn
  },
  props: {
    quesList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      checkValue: {},
      curIcon: curIcon,
      noChose: false,
      validateObjs: {}
    };
  },
  computed: {
    active() {
      return this.$store.getters.active;
    },
    assessForm() {
      return this.$store.getters.assessForm;
    },
    uSex() {
      return this.$store.getters.userSex;
    }
  },
  methods: {
    choseList(key, validate) {
      // value 为键值对，validate为校验函数
      this.validateObjs[key] = validate;
    },
    async nextBtn() {
      let submitData = this.assessForm;
      // 所有的问题都必须返回validate函数，验证函数通过后返回该选项的键值对
      for (let i = 0; i < this.quesList.length; i++) {
        let item = this.quesList[i];
        if (!this.validateObjs[item.key]) {
          uni.showToast({ title: `请选择${item.name}`, icon: 'none' });
          return false;
        }
      }
      let answerObjs = {};
      let keys = Object.keys(this.validateObjs);
      for (let key of keys) {
        let result = this.validateObjs[key]();
        answerObjs = { ...answerObjs, ...result };
      }

      console.log('answerObjs:', answerObjs);

      let healthData = await ajaxSend(answerObjs);
      console.log('healthData:', healthData);

      submitData = Object.assign(submitData, answerObjs);
      wx.pageScrollTo({
        scrollTop: 0
      });

      if (healthData && healthData.length > 0) {
        // 如果有异常，则提示
        this.$store.commit('assessQuestion/SET_AJAXRESMESSAGE', healthData); // 存提示信息到vux，数组格式
        this.$store.commit('assessQuestion/SET_AJAXRES', true); // 开启提示
        wx.pageScrollTo({
          scrollTop: 0
        });
      } else {
        this.$store.commit('assessQuestion/SET_ACTIVE', 4);
        this.$store.commit('assessQuestion/SET_AJAXRES', false);
      }
      // 获取健康指标指导意见 判断是出健康提示还是直接下一步 end
      this.$store.commit('assessQuestion/SET_ASSESSFORM', submitData);
    },
    proBtn() {
      // let curActive = 0;
      // if (this.uSex && this.uSex === '男' && this.active === 8) {
      //   curActive = this.active - 2;
      // } else {
      //   curActive = this.active - 1;
      // }
      // curActive = this.active - 1;
      this.$store.commit('assessQuestion/SET_ACTIVE', 2);
    }
  }
};
</script>
<style lang="scss">
.comBox {
  .title {
    background: rgba(216, 246, 214, 1);
    color: rgba(0, 0, 0, 0.55);
    font-size: 32rpx;
    padding: 14rpx;
    text-align: center;
  }
}
.clear {
  clear: both;
  overflow: hidden;
  zoom: 1;
  height: 0px;
}
.lineHeight86 {
  height: 86rpx;
  line-height: 86rpx;
}
.size28 {
  font-size: 28rpx;
}
.titleBold {
  font-weight: 500;
}
</style>
